//
//  OADTableModelViewController.m
//  OASDK_Works
//
//  Created by ChenJie on 3/1/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADTableModelViewController.h"
#import "UITableView+OADTableConfig.h"
#import "OADTitleContentCell.h"
#import "OADOneCell.h"
#import "OADTwoCell.h"

@interface OADTableModelViewController ()

@property (nonatomic, strong) NSMutableArray *cells;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation OADTableModelViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
    //
    [self buildDataSource];
    
    [self.view addSubview:self.tableView];
    
    NSLog(@"finish add it..");
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGFloat topMark = 0;
    CGSize viewSize = self.view.bounds.size;
    
    self.tableView.frame = CGRectMake(0, topMark, viewSize.width, viewSize.height - topMark);
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Public methods



#pragma mark - Private methods

#define kText

- (NSString *)randomText {
    
    NSString *fullText = @"在营收上，爱奇艺290亿元的营收中有144亿来自会员收益，同比增长了36%，不过会员数1.07亿，其中Q4季度仅增长了22%。\n\
    \n\
    在这样的情况下，爱奇艺要想扭转财务情况，除了继续压缩内容成本之外，在会员身上多收费也是少不了的。\n\
    \n\
    在财报会议上，CEO龚宇表示2020年会减少会员费用促销，提高会员价格，以提升整体ARPU值。\n\
    \n\
    在此之前，爱奇艺已经多次表示国内的视频会员收费太低，去年11月爱奇艺会员及海外业务群总裁杨向华在采访中就提到了国内外会员收费的差距问题。\n\
    \n\
    杨向华表示，根据爱奇艺做的调查，中国市场上的视频会员价格大概只有美国平台价格的五分之一，甚至东南亚国家，比如泰国、越南，他们的会员单价都比中国视频网站的会员价格要高。\n\
    \n\
    在这次财报会议上，爱奇艺正式宣布会员会涨价了，现在还没具体细节放出，但是疫情过后，视频行业会员涨价以及超前点播等额外收费方式肯定会是趋势了。";
    
    NSInteger textLengh = arc4random() % fullText.length;
    return [fullText substringToIndex:textLengh];
}

- (void)buildDataSource {
        
    // section 1
    OADSectionConfig *section_1 = [[OADSectionConfig alloc] init];
    for (NSInteger i = 0; i < 1; i++) {
        OADCellConfig *cellConfig = [[OADCellConfig alloc] init];
        cellConfig.cellClass = OADTwoCell.class;
//        cellConfig.staticCellHeight = 100;
        
        OADTwoCellViewModel *cellVM = [OADTwoCellViewModel new];
        cellConfig.cellViewModel = cellVM;
        
//        OADTitleContentCellViewModel *cellViewModel = [OADTitleContentCellViewModel new];
//        cellViewModel.iconName = @"iconCompany";
//        cellViewModel.title = [self randomText];
//        cellViewModel.content = [NSString stringWithFormat:@"Section 1 - Content %ld", (long)i];
//        cellConfig.cellViewModel = cellViewModel;
        
        [section_1.cells addObject:cellConfig];
    }
    
    [self.tableView.oa_tableConfig_.sections addObject:section_1];
    
    // section 2
//    OADSectionConfig *section_2 = [[OADSectionConfig alloc] init];
//    for (NSInteger i = 0; i < 5; i++) {
//        OADCellConfig *cellConfig = [[OADCellConfig alloc] init];
//        cellConfig.cellClass = OADTitleContentCell.class;
////        cellConfig.constantCellHeight = 250;
//        
//        OADTitleContentCellViewModel *cellViewModel = [OADTitleContentCellViewModel new];
//        cellViewModel.iconName = @"iconManagement";
//        cellViewModel.title = @"起来后就\n\n\n\n时NSLog(format,__VA_ARGS__)了，这就是NSLog的方法了，但是不知道有没有人发现一个细节，如果__VA_ARGS__为空的话，那岂不是成了NSLog(format,)这样肯定会编译报错的，但是苹";
//        cellViewModel.content = [NSString stringWithFormat:@"Section 2 - Content %ld", (long)i];;
//        cellConfig.cellViewModel = cellViewModel;
//
//        [section_2.cells addObject:cellConfig];
//    }
//    [self.tableView.oa_tableConfig.sections addObject:section_2];
    
}

- (Class)cellClassAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    Class cellClass = cellInfo[@"class"];
    return cellClass ?: UITableViewCell.class;
}

- (NSString *)titleAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    return cellInfo[@"title"];
}

- (UIViewController *)targetViewControllerAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    Class targetVcClass = cellInfo[@"target"] ?: UIViewController.class;
    if ([targetVcClass isKindOfClass:NSString.class]) {
        targetVcClass = NSClassFromString((NSString *)targetVcClass);
    }
    
    UIViewController *targetVc = [[targetVcClass alloc] init];
    if ([targetVc isMemberOfClass:UIViewController.class]) {
        targetVc.view.backgroundColor = [UIColor whiteColor];
        targetVc.navigationItem.title = NSStringFromClass(targetVc.class);
    }
    
    return targetVc;
}

#pragma mark - Event response methods

- (void)buttonClicked:(UIButton *)button {
    NSLog(@"button clicked...");
}

#pragma mark - Deletate methods



//#pragma mark - <UITableViewDataSource>
//
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return [tableView oa_numberOfSectionsInTableView];
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return [tableView oa_numberOfRowsInSection:section];
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return [tableView oa_cellForRowAtIndexPath:indexPath];
//}
//
//#pragma mark - <UITableViewDelegate>
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return [tableView oa_heightForRowAtIndexPath:indexPath];
//}
//
//- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
//    return [tableView oa_estimatedHeightForRowAtIndexPath:indexPath];
//}
//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//
////    UIViewController *vc = [self targetViewControllerAtIndexPath:indexPath];
////    [self.navigationController pushViewController:vc animated:YES];
//}

#pragma mark - Setter



#pragma mark - Getter

- (NSMutableArray *)cells {
    if (!_cells) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:3];
        _cells = array;
    }
    return _cells;
}

- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.tableFooterView = [UIView new];
        tableView.estimatedRowHeight = 0;
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - Memory management

- (void)dealloc {
}

@end
